import React from "react";
import style from "./style.module.css";
import { Form, Input, Button } from "antd";
import axios from "axios";

function LoginPage(props) {
  const onFinish = (e) => {
    axios.post("/api/login", e).then((res) => {
      // 存入token
      localStorage.setItem("token", res.data.token);
      props.history.push("/");
      console.log(res);
    });
  };

  const onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };

  return (
    <div className={style.LoginBox}>
      <div className={style.box}>
        <h3>账号密码登录</h3>
        <Form
          labelCol={{ span: 6 }}
          wrapperCol={{ span: 16 }}
          name="basic"
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            label="用户名"
            name="username"
            rules={[
              {
                required: false,
                message: "Please input your username!",
              },
            ]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="密码"
            name="password"
            rules={[
              {
                required: false,
                message: "Please input your password!",
              },
            ]}
          >
            <Input.Password />
          </Form.Item>

          <Form.Item
            wrapperCol={{
              offset: 8,
              span: 16,
            }}
          >
            <Button type="primary" htmlType="submit" className={style.LoginBtn}>
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}

export default LoginPage;
